package com.zmn.plat.admin.controller.contract.record;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.dubbo.config.annotation.Reference;
import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dictionary.DictModel;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.math.MathUtil;
import com.zmn.common.utils.response.ResponseUtil;
import com.zmn.manager.common.database.annotation.ReadOnlyConnection;
import com.zmn.mcc.common.constant.DpermitConsts;
import com.zmn.mcc.common.dto.dept.DeptDRO;
import com.zmn.mcc.dubbo.interfaces.dept.DeptListRemoteService;
import com.zmn.mcc.dubbo.interfaces.staff.StaffListRemoteService;
import com.zmn.plat.admin.controller.PlatBaseController;
import com.zmn.plat.business.interfaces.contract.ContractRecordBService;
import com.zmn.plat.common.constant.CompanyConsts;
import com.zmn.plat.dubbo.utils.DubboConsts;
import com.zmn.plat.manager.dictionary.MasterDict;
import com.zmn.plat.model.entity.company.CompanyTypeQuery;
import com.zmn.plat.model.entity.contract.record.ContractRecord;
import com.zmn.plat.services.interfaces.company.BaseCompanyService;
import com.zmn.plat.services.interfaces.contract.file.record.ContractFileRecordService;
import com.zmn.plat.services.interfaces.contract.record.ContractRecordService;

/**
 * 合同表
 *
 * @author linfeng
 * @since 2020-05-09 11:41:45
 */
public class ContractRecordBaseController extends PlatBaseController {
    protected Logger logger = LoggerFactory.getLogger(this.getClass());
    @Resource
    protected ContractRecordService contractRecordService;

    @Resource
    protected BaseCompanyService baseCompanyService;

    @Reference(version = DubboConsts.INTERFACE_VERSION, check = false)
    protected StaffListRemoteService staffListRemoteService;

    @Resource
    protected ContractFileRecordService contractFileRecordService;

    @Resource
    protected ContractRecordBService contractRecordBService;

    @Reference(version = DubboConsts.INTERFACE_VERSION, check = false)
    DeptListRemoteService deptListRemoteService;

    /**
     * 获取一级部门
     *
     * @return the one dept list
     */
    @ReadOnlyConnection
    protected List<DeptDRO> getOneDeptList() {
        ResponseDTO<List<DeptDRO>> data = deptListRemoteService.listDeptByLevelAndStatus(DpermitConsts.DEPT_LEVEL_ONE, GlobalConsts.YES);
        List<DeptDRO> oneDeptList = new ArrayList<>();
        if (data.isSuccess()) {
            oneDeptList = data.getData();
        }

        return oneDeptList;
    }

    /**
     * 获取二级部门
     *
     * @return the two dept list
     */
    @ReadOnlyConnection
    protected List<DeptDRO> getTwoDeptList() {
        ResponseDTO<List<DeptDRO>> data = deptListRemoteService.listDeptByLevelAndStatus(DpermitConsts.DEPT_LEVEL_TWO, GlobalConsts.YES);
        List<DeptDRO> twoDeptList = new ArrayList<>();
        if (data.isSuccess()) {
            twoDeptList = data.getData();
        }

        return twoDeptList;
    }

    protected List<Integer> getAgentAndAlertReceiverStaffIds(ContractRecord contractRecord) {
        List<Integer> ids = new ArrayList<>();
        if (contractRecord == null) {
            return ids;
        }

        if (contractRecord.getAgent() != null) {
            ids.add(contractRecord.getAgent());
        }

        if (contractRecord.getFirstAlertReceiver1() != null) {
            ids.add(contractRecord.getFirstAlertReceiver1());
        }
        if (contractRecord.getFirstAlertReceiver2() != null) {
            ids.add(contractRecord.getFirstAlertReceiver2());
        }
        if (contractRecord.getFirstAlertReceiver3() != null) {
            ids.add(contractRecord.getFirstAlertReceiver3());
        }
        if (contractRecord.getSecondAlertReceiver1() != null) {
            ids.add(contractRecord.getSecondAlertReceiver1());
        }
        if (contractRecord.getSecondAlertReceiver2() != null) {
            ids.add(contractRecord.getSecondAlertReceiver2());
        }
        if (contractRecord.getSecondAlertReceiver3() != null) {
            ids.add(contractRecord.getSecondAlertReceiver3());
        }

        if (contractRecord.getOverdueAlertReceiver1() != null) {
            ids.add(contractRecord.getOverdueAlertReceiver1());
        }
        if (contractRecord.getOverdueAlertReceiver2() != null) {
            ids.add(contractRecord.getOverdueAlertReceiver2());
        }
        if (contractRecord.getOverdueAlertReceiver3() != null) {
            ids.add(contractRecord.getOverdueAlertReceiver3());
        }

        return ids;
    }

    void listCompanys(HttpServletResponse response, Integer staffId) {

        if (staffId <= 0) {
            ResponseUtil.outWrite(response, "[]");
            return;
        }

        List<CompanyTypeQuery> companyTypeList =
            Arrays.asList(new CompanyTypeQuery(CompanyConsts.COMPANY_TYPE_SUBSIDIARY, CompanyConsts.COMPANY_TYPE_CHILD_DEFAULT),
                new CompanyTypeQuery(CompanyConsts.COMPANY_TYPE_HEADOFFICE, CompanyConsts.COMPANY_TYPE_CHILD_DEFAULT));
        List<DictModel> items = baseCompanyService.listByTypes(companyTypeList, null);

        StringBuilder sb = new StringBuilder("[");
        String jsonFormat = "[\"%s\",\"%s\"]%s ";

        for (int i = 0; i < items.size(); i++) {
            DictModel item = items.get(i);
            sb.append(String.format(jsonFormat, item.getKey(), item.getValue(), (i == items.size() - 1 ? "" : ",")));
        }
        sb.append("]");

        ResponseUtil.outWrite(response, sb.toString());
    }

    protected String narrow(Object dividend) {
        if (Objects.isNull(dividend)) {
            return "";
        }
        double div = MathUtil.div(dividend.toString(), String.valueOf(MasterDict.COEFFICIENT_MAGNIFICATION), 2);
        BigDecimal b = new BigDecimal(div);
        return b.setScale(2, BigDecimal.ROUND_HALF_UP).toString();
    }
}
